package com.iyue.activity;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.iyue.SpringHelper;
import com.iyue.activity.helper.ActivityFunctions;
import com.iyue.activity.helper.ActivityUtil;
import com.iyue.activity.pojo.ActivityAreaCategory;
import com.iyue.activity.pojo.ActivityCategory;
import com.iyue.activity.pojo.ActivityInfo;
import com.iyue.activity.pojo.ActivityPost;
import com.iyue.activity.service.ActivityBiz;
import com.iyue.activity.service.PostBiz;
import com.iyue.framework.BaseAction;
import com.iyue.framework.util.Pager;
import com.iyue.framework.util.PagerHelper;
import com.iyue.servlet.taglibs.Functions;
import com.iyue.sys.pojo.SysProvince;
import com.iyue.user.helper.UserFunctions;
import com.iyue.user.pojo.UserBasic;

/**
 * 《活动首页》实现类
 *
 * @author 孙晓晔
 * @version $Id: Portal.java,v 1.2 2011/01/05 09:24:31 fresh Exp $
 */
public class Activity extends ActivityUtil {

	/*
	 * 网站首页
	 */
	public ActionForward layout(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			BaseAction action) throws Exception {

		Redirect(request, response, "/portal.do?do=Portal.layout");

		return null;
	}

	/**
	 * 移动
	 *
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param action
	 * @return
	 * @throws Exception
	 */
	public ActionForward cmcc(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			BaseAction action) throws Exception {
		UserBasic user = user(request);
		request.setAttribute("cuser", user);
		String cid = request.getParameter("cid");
		if (!StringUtils.isBlank(cid) && StringUtils.isNumeric(cid)) {
			ActivityCategory ac = ActivityFunctions.category(new Integer(cid));
			if (ac == null) {
				request.setAttribute("msg", "参数错误");
				back(request, "/activity.do?do=Activity.cmcc");
				return action.display(request, response, "info");
			}

			Integer level = ac.getLevel();
			if (level.equals(2)) {
				request.setAttribute("pid", cid);
			}

			if (level.equals(3)) {
				Integer pid = ac.getPid();
				request.setAttribute("pid", pid);
				request.setAttribute("cid", cid);
			}

		} else {
			cid = ActivityCategory.CATEGORY_CMCC;
		}

		String ms = request.getParameter("ms");
		if (!StringUtils.isBlank(ms)) {
			request.setAttribute("msg", "请选择品牌");
		}

		StringBuffer p3 = new StringBuffer();
		p3.append("&amp;cid=" + cid);
		String ex = request.getParameter("expire");
		if (StringUtils.isBlank(ex)) {
			ex = "0";
		}

		p3.append("&amp;expire=" + ex);

		String city = "0";
		Integer username = u(request);
		if (username != null) {
			UserBasic basic = UserFunctions.user(username.toString());
			if (basic != null) {
				city = basic.getCity();
				if (!StringUtils.isBlank(city)) {
					p3.append("&amp;city=");
					p3.append(city);
					request.setAttribute("city", city);
				}
			}
		}

		request.setAttribute("p3", p3.toString());

		ActivityBiz biz = (ActivityBiz) SpringHelper.getBean("ActivityBiz");

		int totalRows = 0;
		String s = request.getParameter("n");
		if (s != null && !"".equals(s)) {
			totalRows = Integer.parseInt(s);
		} else {
			totalRows = biz.getCountByCC(city, cid, ex);
		}

		if (totalRows == 0) {
			return action.display(request, response, "cmcc");
		}

		Pager pager = PagerHelper.getPager(request, totalRows, 10);

		List<ActivityAreaCategory> result = biz.getMActivityByCc(city, cid, ex,
				pager);

		request.setAttribute("result", result);
		request.setAttribute("pager", pager);
		request.setAttribute("baseLink", "&amp;do=Activity.cmcc");
		// hangl add
		List<SysProvince> province = Functions.provinces();
		request.setAttribute("provinces", province);
		 
		return action.display(request, response, "cmcc");
	}

	/**
	 * 联通
	 */
	public ActionForward unicom(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			BaseAction action) throws Exception {
		UserBasic user = user(request);
		request.setAttribute("cuser", user);
		String cid = request.getParameter("cid");
		if (!StringUtils.isBlank(cid) && StringUtils.isNumeric(cid)) {
			ActivityCategory ac = ActivityFunctions.category(new Integer(cid));
			if (ac == null) {
				request.setAttribute("msg", "参数错误");
				back(request, "/activity.do?do=Activity.unicom");
				return action.display(request, response, "info");
			}

			Integer level = ac.getLevel();
			if (level.equals(2)) {
				request.setAttribute("pid", cid);
			}

			if (level.equals(3)) {
				Integer pid = ac.getPid();
				request.setAttribute("pid", pid);
				request.setAttribute("cid", cid);
			}

		} else {
			cid = ActivityCategory.CATEGORY_UNICOM;
		}

		String ms = request.getParameter("ms");
		if (!StringUtils.isBlank(ms)) {
			request.setAttribute("msg", "请选择品牌");
		}

		StringBuffer p3 = new StringBuffer();
		p3.append("&amp;cid=" + cid);
		String ex = request.getParameter("expire");
		if (StringUtils.isBlank(ex)) {
			ex = "0";
		}

		p3.append("&amp;expire=" + ex);

		String city = "0";
		Integer username = u(request);
		if (username != null) {
			UserBasic basic = UserFunctions.user(username.toString());
			if (basic != null) {
				city = basic.getCity();
				if (!StringUtils.isBlank(city)) {
					p3.append("&amp;city=");
					p3.append(city);
					request.setAttribute("city", city);
				}
			}
		}

		request.setAttribute("p3", p3.toString());

		ActivityBiz biz = (ActivityBiz) SpringHelper.getBean("ActivityBiz");

		int totalRows = 0;
		String s = request.getParameter("n");
		if (s != null && !"".equals(s)) {
			totalRows = Integer.parseInt(s);
		} else {
			totalRows = biz.getCountByCC(city, cid, ex);
		}

		if (totalRows == 0) {
			return action.display(request, response, "unicom");
		}

		Pager pager = PagerHelper.getPager(request, totalRows, 10);

		List<ActivityAreaCategory> result = biz.getMActivityByCc(city, cid, ex,
				pager);

		request.setAttribute("result", result);
		request.setAttribute("pager", pager);
		request.setAttribute("baseLink", "&amp;do=Activity.unicom");
		// hangl add
		List<SysProvince> province = Functions.provinces();
		request.setAttribute("provinces", province);
		 
		return action.display(request, response, "unicom");
	}

	/**
	 * 银行
	 */
	public ActionForward bank(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			BaseAction action) throws Exception {
		UserBasic user = user(request);
		request.setAttribute("cuser", user);
		String cid = request.getParameter("cid");
		if (!StringUtils.isBlank(cid) && StringUtils.isNumeric(cid)) {
			ActivityCategory ac = ActivityFunctions.category(new Integer(cid));
			if (ac == null) {
				request.setAttribute("msg", "参数错误");
				back(request, "/activity.do?do=Activity.bank");
				return action.display(request, response, "info");
			}

			Integer level = ac.getLevel();
			if (level.equals(2)) {
				request.setAttribute("pid", cid);
			}

			if (level.equals(3)) {
				Integer pid = ac.getPid();
				request.setAttribute("pid", pid);
				request.setAttribute("cid", cid);
			}

		} else {
			cid = ActivityCategory.CATEGORY_BANK;
		}

		String ms = request.getParameter("ms");
		if (!StringUtils.isBlank(ms)) {
			request.setAttribute("msg", "请选择银行");
		}

		StringBuffer p3 = new StringBuffer();
		p3.append("&amp;cid=" + cid);
		String ex = request.getParameter("expire");
		if (StringUtils.isBlank(ex)) {
			ex = "0";
		}

		p3.append("&amp;expire=" + ex);

		String city = "0";
		Integer username = u(request);
		if (username != null) {
			UserBasic basic = UserFunctions.user(username.toString());
			if (basic != null) {
				city = basic.getCity();
				if (!StringUtils.isBlank(city)) {
					p3.append("&amp;city=");
					p3.append(city);
					request.setAttribute("city", city);
				}
			}
		}

		request.setAttribute("p3", p3.toString());

		ActivityBiz biz = (ActivityBiz) SpringHelper.getBean("ActivityBiz");

		int totalRows = 0;
		String s = request.getParameter("n");
		if (s != null && !"".equals(s)) {
			totalRows = Integer.parseInt(s);
		} else {
			totalRows = biz.getCountByCC(city, cid, ex);
		}

		if (totalRows == 0) {
			return action.display(request, response, "bank");
		}

		Pager pager = PagerHelper.getPager(request, totalRows, 10);

		List<ActivityAreaCategory> result = biz.getMActivityByCc(city, cid, ex,
				pager);

		request.setAttribute("result", result);
		request.setAttribute("pager", pager);
		request.setAttribute("baseLink", "&amp;do=Activity.bank");
		// hangl add
		List<SysProvince> province = Functions.provinces();
		request.setAttribute("provinces", province);
		 
		return action.display(request, response, "bank");
	}

	/**
	 * 网站
	 */
	public ActionForward net(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			BaseAction action) throws Exception {
		UserBasic user = user(request);
		request.setAttribute("cuser", user);
		String cid = request.getParameter("cid");
		if (!StringUtils.isBlank(cid) && StringUtils.isNumeric(cid)) {
			ActivityCategory ac = ActivityFunctions.category(new Integer(cid));
			if (ac == null) {
				request.setAttribute("msg", "参数错误");
				back(request, "/activity.do?do=Activity.net");
				return action.display(request, response, "info");
			}

			request.setAttribute("cid", cid);

		} else {
			cid = ActivityCategory.CATEGORY_NET;
		}

		StringBuffer p3 = new StringBuffer();
		p3.append("&amp;cid=" + cid);
		String ex = request.getParameter("expire");
		if (StringUtils.isBlank(ex)) {
			ex = "0";
		}

		p3.append("&amp;expire=" + ex);

		String city = "0";
		Integer username = u(request);
		if (username != null) {
			UserBasic basic = UserFunctions.user(username.toString());
			if (basic != null) {
				city = basic.getCity();
				if (!StringUtils.isBlank(city)) {
					p3.append("&amp;city=");
					p3.append(city);
					request.setAttribute("city", city);
				}
			}
		}

		request.setAttribute("p3", p3.toString());

		ActivityBiz biz = (ActivityBiz) SpringHelper.getBean("ActivityBiz");

		int totalRows = 0;
		String s = request.getParameter("n");
		if (s != null && !"".equals(s)) {
			totalRows = Integer.parseInt(s);
		} else {
			totalRows = biz.getCountByCC(city, cid, ex);
		}

		if (totalRows == 0) {
			return action.display(request, response, "net");
		}

		Pager pager = PagerHelper.getPager(request, totalRows, 10);

		List<ActivityAreaCategory> result = biz.getMActivityByCc(city, cid, ex,
				pager);

		request.setAttribute("result", result);
		request.setAttribute("pager", pager);
		request.setAttribute("baseLink", "&amp;do=Activity.net");
		// hangl add
		List<SysProvince> province = Functions.provinces();
		request.setAttribute("provinces", province);
		 
		return action.display(request, response, "net");
	}

	/**
	 * 宽带
	 *
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param action
	 * @return
	 * @throws Exception
	 */
	public ActionForward broadband(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			BaseAction action) throws Exception {
		UserBasic user = user(request);
		request.setAttribute("cuser", user);
		String cid = request.getParameter("cid");
		if (!StringUtils.isBlank(cid) && StringUtils.isNumeric(cid)) {
			ActivityCategory ac = ActivityFunctions.category(new Integer(cid));
			if (ac == null) {
				request.setAttribute("msg", "参数错误");
				back(request, "/activity.do?do=Activity.broadband");
				return action.display(request, response, "info");
			}

			request.setAttribute("cid", cid);

		} else {
			cid = ActivityCategory.CATEGORY_BROADBAND;
		}

		StringBuffer p3 = new StringBuffer();
		p3.append("&amp;cid=" + cid);
		String ex = request.getParameter("expire");
		if (StringUtils.isBlank(ex)) {
			ex = "0";
		}

		p3.append("&amp;expire=" + ex);

		String city = "0";
		Integer username = u(request);
		if (username != null) {
			UserBasic basic = UserFunctions.user(username.toString());
			if (basic != null) {
				city = basic.getCity();
				if (!StringUtils.isBlank(city)) {
					p3.append("&amp;city=");
					p3.append(city);
					request.setAttribute("city", city);
				}
			}
		}

		request.setAttribute("p3", p3.toString());

		ActivityBiz biz = (ActivityBiz) SpringHelper.getBean("ActivityBiz");

		int totalRows = 0;
		String s = request.getParameter("n");
		if (s != null && !"".equals(s)) {
			totalRows = Integer.parseInt(s);
		} else {
			totalRows = biz.getCountByCC(city, cid, ex);
		}

		if (totalRows == 0) {
			return action.display(request, response, "broadband");
		}

		Pager pager = PagerHelper.getPager(request, totalRows, 10);

		List<ActivityAreaCategory> result = biz.getMActivityByCc(city, cid, ex,
				pager);

		request.setAttribute("result", result);
		request.setAttribute("pager", pager);
		request.setAttribute("baseLink", "&amp;do=Activity.broadband");
		// hangl add
		List<SysProvince> province = Functions.provinces();
		request.setAttribute("provinces", province); 
		return action.display(request, response, "broadband");
	}

	/**
	 * 搜索
	 */
	public ActionForward search(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			BaseAction action) throws Exception {

		return action.display(request, response, "search");
	}

	/**
	 * 搜索
	 */
	public ActionForward searchPost(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			BaseAction action) throws Exception {

		return action.display(request, response, "search");
	}

	/**
	 * 往期活动
	 */
	public ActionForward previous(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			BaseAction action) throws Exception {

		u(request);
		UserBasic user = user(request);
		request.setAttribute("cuser", user);
		ActivityBiz biz = (ActivityBiz) SpringHelper.getBean("ActivityBiz");

		int totalRows = 0;
		String s = request.getParameter("n");
		if (s != null && !"".equals(s)) {
			totalRows = Integer.parseInt(s);
		} else {
			totalRows = biz.getExpireCount();
		}

		if (totalRows == 0) {
			return action.display(request, response, "previous");
		}

		Pager pager = PagerHelper.getPager(request, totalRows, 10);

		List<ActivityAreaCategory> result = biz.getExpires(pager);

		request.setAttribute("result", result);
		request.setAttribute("pager", pager);
		request.setAttribute("baseLink", "&amp;do=Activity.previous");
		// hangl add
		List<SysProvince> province = Functions.provinces();
		request.setAttribute("provinces", province);
		 
		return action.display(request, response, "previous");
	}

	public ActionForward view(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			BaseAction action) throws Exception {
		UserBasic user = user(request);
		request.setAttribute("cuser", user);
		String aacid = request.getParameter("aid");
		if (StringUtils.isBlank(aacid) || !StringUtils.isNumeric(aacid)) {
			request.setAttribute("msg", "参数错误!");
			back(request, "portal.do{0}&amp;do=Portal.layout");
			return action.display(request, response, "info");
		}

		ActivityBiz biz = (ActivityBiz) SpringHelper.getBean("ActivityBiz");
		ActivityAreaCategory aac = biz.getActivityAreaCategory(new Integer(
				aacid));
		if (aac == null) {
			request.setAttribute("msg", "该活动已删除!");
			back(request, "portal.do{0}&amp;do=Portal.layout");
			return action.display(request, response, "info");
		}

		String to = request.getParameter("goto");
		ActivityAreaCategory newtext = biz.getNextText(to, aac);
		if (newtext != null) {
			aac = newtext;
		}

		Boolean next = true, prev = true;
		if (to != null && to.equals("next") && newtext == null) {
			next = false;
		}
		if (to != null && to.equals("prev") && newtext == null) {
			prev = false;
		}
		request.setAttribute("next", next);
		request.setAttribute("prev", prev);

		Integer aid = aac.getAid();
		ActivityInfo text = biz.getActivityInfo(aid);
		if (text == null) {
			request.setAttribute("msg", "参数错误!");
			back(request, "portal.do{0}&amp;do=Portal.layout");
			return action.display(request, response, "info");
		}

		getPost(request, text, aac);

		text.setViews(ActivityFunctions.views(aid, text.getViews(), 1));
		request.setAttribute("result", aac);
		request.setAttribute("text", text);
		request.setAttribute("baseLink", "&amp;do=Activity.view");

		return action.display(request, response, "view");
	}

	/**
	 * 得到回复
	 *
	 * @param request
	 * @param text
	 * @param aac
	 */
	private void getPost(HttpServletRequest request, ActivityInfo text,
			ActivityAreaCategory aac) {
		if (aac == null || text == null) {
			return;
		}

		Integer aid = aac.getAid();

		PostBiz post_biz = (PostBiz) SpringHelper.getBean("PostBiz");
		int totalRows = post_biz.getCount(aid);
		if (text.getReplies() != totalRows) {
			ActivityBiz biz = (ActivityBiz) SpringHelper.getBean("ActivityBiz");
			biz.update_activity_replies(aid, totalRows);
		}

		Pager pager = PagerHelper.getPager(request, totalRows, 8);
		List<ActivityPost> posts = post_biz.getPosts(aid, pager);
		StringBuffer sb = new StringBuffer();
		sb.append("&amp;aid=");
		sb.append(aac.getId());

		request.setAttribute("p3", sb.toString());
		request.setAttribute("posts", posts);
		request.setAttribute("pager", pager);
	}

}
